package com.vhans.db.service.impl;

import cn.hutool.core.lang.Assert;
import com.vhans.core.domain.BandwidthGroups;
import com.vhans.core.utils.BeanCopyUtils;
import com.vhans.db.mapper.BandwidthGroupsMapper;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.dto.bandwidthgroups.BandwidthGroupsDTO;
import com.vhans.db.model.vo.bandwidthgroups.BandwidthGroupsInfoVO;
import com.vhans.db.model.vo.bandwidthgroups.BandwidthGroupsVO;
import com.vhans.db.service.IBandwidthGroupsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * 带宽和安全组Service业务层处理
 * 
 * @author vhans
 * @date 2023-05-20
 */
@Service
public class BandwidthGroupsServiceImpl implements IBandwidthGroupsService {
    @Autowired
    private BandwidthGroupsMapper bandwidthGroupsMapper;

    /**
     * 查询带宽和安全组列表
     * 
     * @param condition 条件
     * @return 带宽和安全组
     */
    @Override
    public List<BandwidthGroupsVO> selectBandwidthGroupsVOList(ConditionDTO condition) {
        // 查询带宽和安全组信息
        return bandwidthGroupsMapper.selectBandwidthGroupsVOList(condition);
    }

    /**
     * 新增带宽和安全组
     * 
     * @param bandwidthGroups 带宽和安全组
     * @return 影响行
     */
    @Override
    public Integer insertBandwidthGroups(BandwidthGroupsDTO bandwidthGroups) {
        // 要添加的带宽和安全组
        BandwidthGroups newBandwidthGroups = BeanCopyUtils.copyBean(bandwidthGroups, BandwidthGroups.class);
        // 赋值ip
        newBandwidthGroups.setIp(generateRandomIP());
        bandwidthGroupsMapper.insert(newBandwidthGroups);
        return newBandwidthGroups.getId();
    }

    /**
     * 获取带宽和安全组编辑信息
     *
     * @param id 带宽和安全组主键
     * @return 带宽和安全组
     */
    @Override
    public BandwidthGroupsInfoVO selectBandwidthGroupsInfoById(Integer id) {
        // 查询带宽和安全组信息
        BandwidthGroupsInfoVO bandwidthGroupsInfoVO = bandwidthGroupsMapper.selectBandwidthGroupsInfoById(id);
        Assert.notNull(bandwidthGroupsInfoVO, "没有该带宽和安全组");
        return bandwidthGroupsInfoVO;
    }

    /**
     * 修改带宽和安全组
     * 
     * @param bandwidthGroups 带宽和安全组
     */
    @Override
    public void updateBandwidthGroups(BandwidthGroupsDTO bandwidthGroups) {
        // 要修改的带宽和安全组
        BandwidthGroups newBandwidthGroups = BeanCopyUtils.copyBean(bandwidthGroups, BandwidthGroups.class);
        bandwidthGroupsMapper.updateById(newBandwidthGroups);
    }

    /**
     * 批量删除带宽和安全组
     *
     * @param idList 需要删除的带宽和安全组主键列表
     * @return 影响行
     */
    @Override
    public int deleteBandwidthGroupsByIdIdList(List<Integer> idList) {
        // 删除带宽和安全组
        return bandwidthGroupsMapper.deleteBatchIds(idList);
    }

    /**
     * 生成ip
     * @return ip
     */
    private String generateRandomIP() {
        Random random = new Random();
        int[] segments = new int[4];
        for (int i = 0; i < 4; i++) {
            segments[i] = random.nextInt(256); // 生成范围在 0 到 255 之间的随机数
        }
        return String.format("%d.%d.%d.%d", segments[0], segments[1], segments[2], segments[3]);
    }
}
